package hsp.admin.app.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hsp.admin.app.base.mapper.UserRoleRelationMapper;
import hsp.admin.app.base.po.UserRoleRelationPO;
import hsp.admin.app.base.service.IUserRoleRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Service
public class UserRoleRelationServiceImpl extends ServiceImpl<UserRoleRelationMapper, UserRoleRelationPO> implements IUserRoleRelationService {

    @Transactional
    @Override
    public void saveBatch(long userId, long[] roleIds) {
        LambdaQueryWrapper<UserRoleRelationPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleRelationPO::getUserId, userId);
        remove(wrapper);

        if(roleIds == null) return;
        ArrayList<UserRoleRelationPO> list = new ArrayList<>();
        for (long roleId: roleIds){
            UserRoleRelationPO relation = new UserRoleRelationPO();
            relation.setUserId(userId);
            relation.setRoleId(roleId);
            list.add(relation);
        }
        saveBatch(list);
    }

}
